<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Java 基础 - 知识点 | 一码编程</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/img/favicon.ico">
    <script data-ad-client="ca-pub-7828333725993554" async="async" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <meta name="description" content="web前端技术博客,简洁至上,专注web前端学习与总结。JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github等技术文章。">
    <meta name="keywords" content="Java 知识体系,个人技术博客,后端,后端开发,后端框架,web前端,后端面试题,技术文档,学习,面试,Spring,Java,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.8c0de3bf.css" as="style"><link rel="preload" href="/assets/js/app.66ec18f0.js" as="script"><link rel="preload" href="/assets/js/2.94569d7c.js" as="script"><link rel="preload" href="/assets/js/79.0e8351bf.js" as="script"><link rel="prefetch" href="/assets/js/10.9f6df3f3.js"><link rel="prefetch" href="/assets/js/100.90b5eafa.js"><link rel="prefetch" href="/assets/js/101.e7cf2503.js"><link rel="prefetch" href="/assets/js/102.132ee4da.js"><link rel="prefetch" href="/assets/js/103.c0457c15.js"><link rel="prefetch" href="/assets/js/104.7355256c.js"><link rel="prefetch" href="/assets/js/105.90c948fd.js"><link rel="prefetch" href="/assets/js/106.4ce0d125.js"><link rel="prefetch" href="/assets/js/107.b18bd269.js"><link rel="prefetch" href="/assets/js/108.3bed0655.js"><link rel="prefetch" href="/assets/js/109.40f361d8.js"><link rel="prefetch" href="/assets/js/11.a83bdce3.js"><link rel="prefetch" href="/assets/js/110.d452c033.js"><link rel="prefetch" href="/assets/js/111.fc0320ab.js"><link rel="prefetch" href="/assets/js/112.f149f0fc.js"><link rel="prefetch" href="/assets/js/113.8f2e4a42.js"><link rel="prefetch" href="/assets/js/114.14bae374.js"><link rel="prefetch" href="/assets/js/115.9cdc00eb.js"><link rel="prefetch" href="/assets/js/116.e473e37b.js"><link rel="prefetch" href="/assets/js/117.8e77ea25.js"><link rel="prefetch" href="/assets/js/118.3db4ad27.js"><link rel="prefetch" href="/assets/js/119.cbf28d7b.js"><link rel="prefetch" href="/assets/js/12.95a37db8.js"><link rel="prefetch" href="/assets/js/120.94bba4c6.js"><link rel="prefetch" href="/assets/js/121.5515ee13.js"><link rel="prefetch" href="/assets/js/122.fc2a0ae7.js"><link rel="prefetch" href="/assets/js/123.de0f3682.js"><link rel="prefetch" href="/assets/js/124.d835e05f.js"><link rel="prefetch" href="/assets/js/125.8c9d9f85.js"><link rel="prefetch" href="/assets/js/126.01ef7d9d.js"><link rel="prefetch" href="/assets/js/127.61e602a5.js"><link rel="prefetch" href="/assets/js/128.c7d8277c.js"><link rel="prefetch" href="/assets/js/129.245b12e4.js"><link rel="prefetch" href="/assets/js/13.1acbcf03.js"><link rel="prefetch" href="/assets/js/130.56a62f57.js"><link rel="prefetch" href="/assets/js/131.dabeff40.js"><link rel="prefetch" href="/assets/js/132.7c898663.js"><link rel="prefetch" href="/assets/js/133.fe172050.js"><link rel="prefetch" href="/assets/js/134.ed97034b.js"><link rel="prefetch" href="/assets/js/135.b21206db.js"><link rel="prefetch" href="/assets/js/136.9c949bb5.js"><link rel="prefetch" href="/assets/js/137.ca167f36.js"><link rel="prefetch" href="/assets/js/138.78397eaf.js"><link rel="prefetch" href="/assets/js/139.4bb796c1.js"><link rel="prefetch" href="/assets/js/14.d4f8ea8f.js"><link rel="prefetch" href="/assets/js/140.e1a6fba1.js"><link rel="prefetch" href="/assets/js/141.c301b64c.js"><link rel="prefetch" href="/assets/js/142.0c707d5f.js"><link rel="prefetch" href="/assets/js/143.1ec2981e.js"><link rel="prefetch" href="/assets/js/144.a57572b6.js"><link rel="prefetch" href="/assets/js/145.350cbe8a.js"><link rel="prefetch" href="/assets/js/146.c050e9e0.js"><link rel="prefetch" href="/assets/js/147.10ee482a.js"><link rel="prefetch" href="/assets/js/148.0fc834f1.js"><link rel="prefetch" href="/assets/js/149.77dd57bf.js"><link rel="prefetch" href="/assets/js/15.ce7515db.js"><link rel="prefetch" href="/assets/js/150.4fc76d42.js"><link rel="prefetch" href="/assets/js/151.73c875ec.js"><link rel="prefetch" href="/assets/js/152.e0b6c00d.js"><link rel="prefetch" href="/assets/js/153.6c120d67.js"><link rel="prefetch" href="/assets/js/154.01051fe1.js"><link rel="prefetch" href="/assets/js/155.2a17fb22.js"><link rel="prefetch" href="/assets/js/156.ada61aaa.js"><link rel="prefetch" href="/assets/js/157.248dc039.js"><link rel="prefetch" href="/assets/js/158.0d5f94bd.js"><link rel="prefetch" href="/assets/js/159.9c3deeda.js"><link rel="prefetch" href="/assets/js/16.4b68c1d3.js"><link rel="prefetch" href="/assets/js/160.d95c292d.js"><link rel="prefetch" href="/assets/js/161.56f971fa.js"><link rel="prefetch" href="/assets/js/162.232ce423.js"><link rel="prefetch" href="/assets/js/163.92323a72.js"><link rel="prefetch" href="/assets/js/164.275aa708.js"><link rel="prefetch" href="/assets/js/165.d47cde30.js"><link rel="prefetch" href="/assets/js/166.c8299909.js"><link rel="prefetch" href="/assets/js/167.974fe7cf.js"><link rel="prefetch" href="/assets/js/168.18a8c64f.js"><link rel="prefetch" href="/assets/js/169.b2adb3d2.js"><link rel="prefetch" href="/assets/js/17.e586f917.js"><link rel="prefetch" href="/assets/js/170.9d53a901.js"><link rel="prefetch" href="/assets/js/171.5a65edf7.js"><link rel="prefetch" href="/assets/js/172.44a7f551.js"><link rel="prefetch" href="/assets/js/173.19ca9e62.js"><link rel="prefetch" href="/assets/js/174.7d4d7be5.js"><link rel="prefetch" href="/assets/js/175.6aa7939a.js"><link rel="prefetch" href="/assets/js/176.b82e1645.js"><link rel="prefetch" href="/assets/js/177.8c9727b0.js"><link rel="prefetch" href="/assets/js/178.6c4fa6f0.js"><link rel="prefetch" href="/assets/js/179.91817012.js"><link rel="prefetch" href="/assets/js/18.f8479181.js"><link rel="prefetch" href="/assets/js/180.bdded9da.js"><link rel="prefetch" href="/assets/js/181.39ee6cdc.js"><link rel="prefetch" href="/assets/js/182.7a736049.js"><link rel="prefetch" href="/assets/js/183.b3f14f77.js"><link rel="prefetch" href="/assets/js/184.739caa46.js"><link rel="prefetch" href="/assets/js/185.f4abf18e.js"><link rel="prefetch" href="/assets/js/186.91a57d34.js"><link rel="prefetch" href="/assets/js/187.6694e5f5.js"><link rel="prefetch" href="/assets/js/188.b4bd2a24.js"><link rel="prefetch" href="/assets/js/189.488eaf65.js"><link rel="prefetch" href="/assets/js/19.270533e4.js"><link rel="prefetch" href="/assets/js/190.2f3c9e73.js"><link rel="prefetch" href="/assets/js/191.c8bff233.js"><link rel="prefetch" href="/assets/js/192.4f05c3b5.js"><link rel="prefetch" href="/assets/js/193.24b7b232.js"><link rel="prefetch" href="/assets/js/194.89ab4683.js"><link rel="prefetch" href="/assets/js/195.49ed7290.js"><link rel="prefetch" href="/assets/js/196.025c381d.js"><link rel="prefetch" href="/assets/js/197.c4f8a6b1.js"><link rel="prefetch" href="/assets/js/198.cc687156.js"><link rel="prefetch" href="/assets/js/199.c842f262.js"><link rel="prefetch" href="/assets/js/20.6f9d7815.js"><link rel="prefetch" href="/assets/js/200.15fb0a60.js"><link rel="prefetch" href="/assets/js/201.b3f4f118.js"><link rel="prefetch" href="/assets/js/202.cedfcb88.js"><link rel="prefetch" href="/assets/js/203.f860c15b.js"><link rel="prefetch" href="/assets/js/204.2d6ce65d.js"><link rel="prefetch" href="/assets/js/205.7c78e462.js"><link rel="prefetch" href="/assets/js/206.cd2f039c.js"><link rel="prefetch" href="/assets/js/207.cccbfddd.js"><link rel="prefetch" href="/assets/js/208.21e75d6f.js"><link rel="prefetch" href="/assets/js/209.65effd68.js"><link rel="prefetch" href="/assets/js/21.1ad2d3c1.js"><link rel="prefetch" href="/assets/js/210.9e71a7f5.js"><link rel="prefetch" href="/assets/js/211.969e72ad.js"><link rel="prefetch" href="/assets/js/212.820f8094.js"><link rel="prefetch" href="/assets/js/213.df5ac78b.js"><link rel="prefetch" href="/assets/js/214.d9b77af5.js"><link rel="prefetch" href="/assets/js/215.08b2a419.js"><link rel="prefetch" href="/assets/js/216.38df1922.js"><link rel="prefetch" href="/assets/js/217.d3933fe8.js"><link rel="prefetch" href="/assets/js/218.eb3fecfe.js"><link rel="prefetch" href="/assets/js/219.7c5c54ad.js"><link rel="prefetch" href="/assets/js/22.8628d841.js"><link rel="prefetch" href="/assets/js/220.263411db.js"><link rel="prefetch" href="/assets/js/221.ea2bc4d9.js"><link rel="prefetch" href="/assets/js/222.e3fc7753.js"><link rel="prefetch" href="/assets/js/223.2658ac0c.js"><link rel="prefetch" href="/assets/js/224.ad59b2ea.js"><link rel="prefetch" href="/assets/js/225.639296c3.js"><link rel="prefetch" href="/assets/js/226.ee8abb8a.js"><link rel="prefetch" href="/assets/js/227.70ba5a31.js"><link rel="prefetch" href="/assets/js/228.217a5ee3.js"><link rel="prefetch" href="/assets/js/229.236c5847.js"><link rel="prefetch" href="/assets/js/23.6f676329.js"><link rel="prefetch" href="/assets/js/230.989f1030.js"><link rel="prefetch" href="/assets/js/231.93917c5d.js"><link rel="prefetch" href="/assets/js/232.84c835d7.js"><link rel="prefetch" href="/assets/js/233.c84a49e1.js"><link rel="prefetch" href="/assets/js/234.d99384c2.js"><link rel="prefetch" href="/assets/js/235.ab6a57aa.js"><link rel="prefetch" href="/assets/js/236.a2c423a6.js"><link rel="prefetch" href="/assets/js/237.e1b8b45c.js"><link rel="prefetch" href="/assets/js/238.ebc7a473.js"><link rel="prefetch" href="/assets/js/239.a13c8156.js"><link rel="prefetch" href="/assets/js/24.c8e5b352.js"><link rel="prefetch" href="/assets/js/240.701e1d60.js"><link rel="prefetch" href="/assets/js/241.fc7fdaad.js"><link rel="prefetch" href="/assets/js/242.50a84697.js"><link rel="prefetch" href="/assets/js/243.3a65c46e.js"><link rel="prefetch" href="/assets/js/244.c2d3da27.js"><link rel="prefetch" href="/assets/js/245.90cf114d.js"><link rel="prefetch" href="/assets/js/246.175e42a8.js"><link rel="prefetch" href="/assets/js/247.c96948b7.js"><link rel="prefetch" href="/assets/js/248.e7c91eb4.js"><link rel="prefetch" href="/assets/js/249.f54451d6.js"><link rel="prefetch" href="/assets/js/25.714b7be8.js"><link rel="prefetch" href="/assets/js/250.e7583b33.js"><link rel="prefetch" href="/assets/js/251.2cd29c41.js"><link rel="prefetch" href="/assets/js/26.bd9da114.js"><link rel="prefetch" href="/assets/js/27.c219321c.js"><link rel="prefetch" href="/assets/js/28.45088c1e.js"><link rel="prefetch" href="/assets/js/29.09bf218e.js"><link rel="prefetch" href="/assets/js/3.39144ebb.js"><link rel="prefetch" href="/assets/js/30.011f667b.js"><link rel="prefetch" href="/assets/js/31.a3145b0e.js"><link rel="prefetch" href="/assets/js/32.61a565cb.js"><link rel="prefetch" href="/assets/js/33.b0e79d8f.js"><link rel="prefetch" href="/assets/js/34.6ce517b2.js"><link rel="prefetch" href="/assets/js/35.a55f3b4b.js"><link rel="prefetch" href="/assets/js/36.5cb6f5ab.js"><link rel="prefetch" href="/assets/js/37.dc818991.js"><link rel="prefetch" href="/assets/js/38.7287d147.js"><link rel="prefetch" href="/assets/js/39.d05c15ae.js"><link rel="prefetch" href="/assets/js/4.ffde19c5.js"><link rel="prefetch" href="/assets/js/40.8e92777a.js"><link rel="prefetch" href="/assets/js/41.9afe0600.js"><link rel="prefetch" href="/assets/js/42.2398ae2a.js"><link rel="prefetch" href="/assets/js/43.42eb6d55.js"><link rel="prefetch" href="/assets/js/44.11c37dde.js"><link rel="prefetch" href="/assets/js/45.ca695a69.js"><link rel="prefetch" href="/assets/js/46.4f3e0b61.js"><link rel="prefetch" href="/assets/js/47.12e82ba7.js"><link rel="prefetch" href="/assets/js/48.75dc7298.js"><link rel="prefetch" href="/assets/js/49.80a6a902.js"><link rel="prefetch" href="/assets/js/5.808dc17c.js"><link rel="prefetch" href="/assets/js/50.a4ae29db.js"><link rel="prefetch" href="/assets/js/51.d9db2fa7.js"><link rel="prefetch" href="/assets/js/52.6a0cab7a.js"><link rel="prefetch" href="/assets/js/53.d20e8f05.js"><link rel="prefetch" href="/assets/js/54.1d01c783.js"><link rel="prefetch" href="/assets/js/55.5193632e.js"><link rel="prefetch" href="/assets/js/56.b2b245c0.js"><link rel="prefetch" href="/assets/js/57.79e68e16.js"><link rel="prefetch" href="/assets/js/58.15f5a489.js"><link rel="prefetch" href="/assets/js/59.5036aeff.js"><link rel="prefetch" href="/assets/js/6.a9f6899c.js"><link rel="prefetch" href="/assets/js/60.6b2f3d1f.js"><link rel="prefetch" href="/assets/js/61.93b24a50.js"><link rel="prefetch" href="/assets/js/62.91a7cb5d.js"><link rel="prefetch" href="/assets/js/63.07900dfe.js"><link rel="prefetch" href="/assets/js/64.bf177ab5.js"><link rel="prefetch" href="/assets/js/65.3bdfad21.js"><link rel="prefetch" href="/assets/js/66.38ff0639.js"><link rel="prefetch" href="/assets/js/67.2d4ca96e.js"><link rel="prefetch" href="/assets/js/68.67315a94.js"><link rel="prefetch" href="/assets/js/69.02ceaaa8.js"><link rel="prefetch" href="/assets/js/7.d6405d52.js"><link rel="prefetch" href="/assets/js/70.6dd6d824.js"><link rel="prefetch" href="/assets/js/71.c322ae43.js"><link rel="prefetch" href="/assets/js/72.8c122a54.js"><link rel="prefetch" href="/assets/js/73.52351bd6.js"><link rel="prefetch" href="/assets/js/74.e63e7849.js"><link rel="prefetch" href="/assets/js/75.ea8d8d16.js"><link rel="prefetch" href="/assets/js/76.6a3cf968.js"><link rel="prefetch" href="/assets/js/77.da0156e4.js"><link rel="prefetch" href="/assets/js/78.1ff3f855.js"><link rel="prefetch" href="/assets/js/8.87819edd.js"><link rel="prefetch" href="/assets/js/80.8ae2bc20.js"><link rel="prefetch" href="/assets/js/81.eff278ac.js"><link rel="prefetch" href="/assets/js/82.b6c26176.js"><link rel="prefetch" href="/assets/js/83.bc76aa23.js"><link rel="prefetch" href="/assets/js/84.12d6e44f.js"><link rel="prefetch" href="/assets/js/85.c5a8e231.js"><link rel="prefetch" href="/assets/js/86.881b4810.js"><link rel="prefetch" href="/assets/js/87.9c91aa9b.js"><link rel="prefetch" href="/assets/js/88.586a37db.js"><link rel="prefetch" href="/assets/js/89.f8a18a98.js"><link rel="prefetch" href="/assets/js/9.0d1d61a0.js"><link rel="prefetch" href="/assets/js/90.bd368869.js"><link rel="prefetch" href="/assets/js/91.c3a90640.js"><link rel="prefetch" href="/assets/js/92.9b1b660c.js"><link rel="prefetch" href="/assets/js/93.ce971fa1.js"><link rel="prefetch" href="/assets/js/94.f7beac4c.js"><link rel="prefetch" href="/assets/js/95.244f4715.js"><link rel="prefetch" href="/assets/js/96.c908a45c.js"><link rel="prefetch" href="/assets/js/97.37c6101f.js"><link rel="prefetch" href="/assets/js/98.651dc399.js"><link rel="prefetch" href="/assets/js/99.31028387.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8c0de3bf.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/EB-logo.png" alt="一码编程" class="logo"> <span class="site-name can-hide">一码编程</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Java" class="dropdown-title"><a href="/java" class="link-title">Java</a> <span class="title" style="display:none;">Java</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>Java 基础</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/ae2dff/" aria-current="page" class="nav-link router-link-exact-active router-link-active">Java 基础知识点</a></li></ul></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">Java 集合框架</a></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">Java 多线程并发</a></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">Java IO/NIO/AIO</a></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">JVM 总结</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/pages/51afd6/" class="nav-link">TypeScript</a></li><li class="dropdown-subitem"><a href="/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/linbingxing/vuepress-theme-vdoing" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://avatars.githubusercontent.com/u/5601844?s=96&amp;v=4"> <div class="blogger-info"><h3>林半码</h3> <span>编程届的痞马</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Java" class="dropdown-title"><a href="/java" class="link-title">Java</a> <span class="title" style="display:none;">Java</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>Java 基础</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/ae2dff/" aria-current="page" class="nav-link router-link-exact-active router-link-active">Java 基础知识点</a></li></ul></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">Java 集合框架</a></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">Java 多线程并发</a></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">Java IO/NIO/AIO</a></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">JVM 总结</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/pages/51afd6/" class="nav-link">TypeScript</a></li><li class="dropdown-subitem"><a href="/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/linbingxing/vuepress-theme-vdoing" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>基础</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/a23428/" class="sidebar-link">Java 基础 - 面向对象</a></li><li><a href="/pages/ae2dff/" aria-current="page" class="active sidebar-link">Java 基础 - 知识点</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/ae2dff/#一、数据类型" class="sidebar-link">一、数据类型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/ae2dff/#基本类型" class="sidebar-link">基本类型</a></li><li class="sidebar-sub-header"><a href="/pages/ae2dff/#包装类型" class="sidebar-link">包装类型</a></li><li class="sidebar-sub-header"><a href="/pages/ae2dff/#缓存池" class="sidebar-link">缓存池</a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/ae2dff/#二、string" class="sidebar-link">二、String</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/ae2dff/#概览" class="sidebar-link">概览</a></li><li class="sidebar-sub-header"><a href="/pages/ae2dff/#不可变的好处" class="sidebar-link">不可变的好处</a></li><li class="sidebar-sub-header"><a href="/pages/ae2dff/#stringbuffer和stringbuilder区别" class="sidebar-link">StringBuffer和StringBuilder区别</a></li><li class="sidebar-sub-header"><a href="/pages/ae2dff/#string-pool" class="sidebar-link">String Pool</a></li><li class="sidebar-sub-header"><a href="/pages/ae2dff/#字符型常量和字符串常量" class="sidebar-link">字符型常量和字符串常量</a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/ae2dff/#三、运算" class="sidebar-link">三、运算</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/ae2dff/#参数传递" class="sidebar-link">参数传递</a></li><li class="sidebar-sub-header"><a href="/pages/ae2dff/#float-与-double" class="sidebar-link">float 与 double</a></li><li class="sidebar-sub-header"><a href="/pages/ae2dff/#隐式类型转换" class="sidebar-link">隐式类型转换</a></li><li class="sidebar-sub-header"><a href="/pages/ae2dff/#switch" class="sidebar-link">switch</a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/ae2dff/#四、关键字" class="sidebar-link">四、关键字</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/ae2dff/#final" class="sidebar-link">final</a></li></ul></li></ul></li><li><a href="/pages/6c6098/" class="sidebar-link">Java 基础 - 反射机制详解</a></li><li><a href="/pages/97c136/" class="sidebar-link">Java 基础 - 泛型机制详解</a></li><li><a href="/pages/b7f470/" class="sidebar-link">Java 基础 - 异常机制详解</a></li><li><a href="/pages/7ff67e/" class="sidebar-link">Java 基础 - 注解机制详解</a></li><li><a href="/pages/e8bbde/" class="sidebar-link">Java常用机制 - SPI机制详解</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>集合</span> <span class="arrow right"></span></p> <!----></section></li></ul> <div class="sidebar-slot sidebar-slot-bottom"><!-- 正方形 -->
      <ins class="adsbygoogle"
          style="display:block"
          data-ad-client="ca-pub-7828333725993554"
          data-ad-slot="3508773082"
          data-ad-format="auto"
          data-full-width-responsive="true"></ins>
      <script>
          (adsbygoogle = window.adsbygoogle || []).push({});
      </script></div></aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-1cd794fe><div class="articleInfo" data-v-1cd794fe><ul class="breadcrumbs" data-v-1cd794fe><li data-v-1cd794fe><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-1cd794fe></a></li> <li data-v-1cd794fe><a href="/java" title="Java-目录页" data-v-1cd794fe>Java</a></li> <li data-v-1cd794fe><a href="/java/#基础" title="Java#基础" data-v-1cd794fe>基础</a></li> <!----></ul> <div class="info" data-v-1cd794fe><div title="作者" class="author iconfont icon-touxiang" data-v-1cd794fe><a href="https://github.com/linbingxing" target="_blank" title="作者" class="beLink" data-v-1cd794fe>半码</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-1cd794fe><a href="javascript:;" data-v-1cd794fe>2021-11-28</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-content"></div></div></div> <h1><img src="">
          Java 基础 - 知识点
        </h1> <div class="page-slot page-slot-top"><!-- 固定100% * 90px可显示，max-height:90px未见显示-->
     <ins class="adsbygoogle"
          style="display:inline-block;width:100%;max-height:90px"
          data-ad-client="ca-pub-7828333725993554"
          data-ad-slot="6625304284"></ins>
      <script>
          (adsbygoogle = window.adsbygoogle || []).push({});
      </script></div> <div class="theme-vdoing-content content__default"><h1 id="java基础-知识点"><a href="#java基础-知识点" class="header-anchor">#</a> Java基础-知识点</h1> <h2 id="一、数据类型"><a href="#一、数据类型" class="header-anchor">#</a> 一、数据类型</h2> <h3 id="基本类型"><a href="#基本类型" class="header-anchor">#</a> 基本类型</h3> <p>Java 中有 8 种基本数据类型，分别为：</p> <ol><li>6 种数字类型 ：<code>byte</code>、<code>short</code>、<code>int</code>、<code>long</code>、<code>float</code>、<code>double</code></li> <li>1 种字符类型：<code>char</code></li> <li>1 种布尔型：<code>boolean</code>。</li></ol> <p>这 8 种基本数据类型的默认值以及所占空间的大小如下：</p> <table><thead><tr><th>基本类型</th> <th>位数</th> <th>字节</th> <th>默认值</th></tr></thead> <tbody><tr><td><code>int</code></td> <td>32</td> <td>4</td> <td>0</td></tr> <tr><td><code>short</code></td> <td>16</td> <td>2</td> <td>0</td></tr> <tr><td><code>long</code></td> <td>64</td> <td>8</td> <td>0L</td></tr> <tr><td><code>byte</code></td> <td>8</td> <td>1</td> <td>0</td></tr> <tr><td><code>char</code></td> <td>16</td> <td>2</td> <td>'u0000'</td></tr> <tr><td><code>float</code></td> <td>32</td> <td>4</td> <td>0f</td></tr> <tr><td><code>double</code></td> <td>64</td> <td>8</td> <td>0d</td></tr> <tr><td><code>boolean</code></td> <td>1</td> <td></td> <td>false</td></tr></tbody></table> <p>另外，对于 <code>boolean</code>，官方文档未明确定义，它依赖于 JVM 厂商的具体实现。逻辑上理解是占用 1 位，但是实际中会考虑计算机高效存储因素。</p> <h3 id="包装类型"><a href="#包装类型" class="header-anchor">#</a> 包装类型</h3> <p>八种基本类型都有对应的包装类，对应如下：</p> <table><thead><tr><th>原始类型</th> <th>封装类型</th></tr></thead> <tbody><tr><td>boolean</td> <td>Boolean</td></tr> <tr><td>char</td> <td>Character</td></tr> <tr><td>byte</td> <td>Byte</td></tr> <tr><td>int</td> <td>Integer</td></tr> <tr><td>long</td> <td>Long</td></tr> <tr><td>float</td> <td>Float</td></tr> <tr><td>double</td> <td>Double</td></tr></tbody></table> <p>基本类型与其对应的包装类型之间的赋值使用自动装箱与拆箱完成。</p> <p><strong>自动装箱</strong>是Java编译器在基本数据类型和对应的对象包装类型之间做的一个转化，比如把int转化为Integer，double转化为Double。反之就是自动拆箱。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Integer</span> x <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>     <span class="token comment">// 装箱 调用了 Integer.valueOf(1)</span>
<span class="token keyword">int</span> y <span class="token operator">=</span> x<span class="token punctuation">;</span>         <span class="token comment">// 拆箱 调用了 X.intValue()</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="缓存池"><a href="#缓存池" class="header-anchor">#</a> 缓存池</h3> <p>Java 基本类型的包装类的大部分都实现了常量池技术。</p> <p><code>Byte</code>,<code>Short</code>,<code>Integer</code>,<code>Long</code> 这 4 种包装类默认创建了数值 <strong>[-128，127]</strong> 的相应类型的缓存数据，</p> <p><code>Character</code> 创建了数值在[0,127]范围的缓存数据，</p> <p><code>Boolean</code> 直接返回 <code>True</code> Or <code>False</code>。</p> <p>new Integer(123) 与 Integer.valueOf(123) 的区别在于：</p> <ul><li>new Integer(123) 每次都会新建一个对象；</li> <li>Integer.valueOf(123) 会使用缓存池中的对象，多次调用会取得同一个对象的引用。</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Integer</span> x <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Integer</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Integer</span> y <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Integer</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>x <span class="token operator">==</span> y<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">// false</span>
<span class="token class-name">Integer</span> z <span class="token operator">=</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Integer</span> k <span class="token operator">=</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>z <span class="token operator">==</span> k<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>valueOf() 方法的实现比较简单，就是先判断值是否在缓存池中，如果在的话就直接返回缓存池的内容。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">Integer</span> <span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">&gt;=</span> <span class="token class-name">IntegerCache</span><span class="token punctuation">.</span>low <span class="token operator">&amp;&amp;</span> i <span class="token operator">&lt;=</span> <span class="token class-name">IntegerCache</span><span class="token punctuation">.</span>high<span class="token punctuation">)</span>
        <span class="token keyword">return</span> <span class="token class-name">IntegerCache</span><span class="token punctuation">.</span>cache<span class="token punctuation">[</span>i <span class="token operator">+</span> <span class="token punctuation">(</span><span class="token operator">-</span><span class="token class-name">IntegerCache</span><span class="token punctuation">.</span>low<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Integer</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>记住：<strong>所有整型包装类对象之间值的比较，全部使用 equals 方法比较</strong>。</p> <h2 id="二、string"><a href="#二、string" class="header-anchor">#</a> 二、String</h2> <h3 id="概览"><a href="#概览" class="header-anchor">#</a> 概览</h3> <p>String 被声明为 final，因此它不可被继承。</p> <p>内部使用 char 数组存储数据，该数组被声明为 final，这意味着 value 数组初始化之后就不能再引用其它数组。并且 String 内部没有改变 value 数组的方法，因此可以保证 String 不可变。</p> <p>在 Java 8 中，String 内部使用 char 数组存储数据。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">class</span> <span class="token class-name">String</span>
    <span class="token keyword">implements</span> <span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span>Serializable</span><span class="token punctuation">,</span> <span class="token class-name">Comparable</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">,</span> <span class="token class-name">CharSequence</span> <span class="token punctuation">{</span>
    <span class="token comment">/** The value is used for character storage. */</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token keyword">char</span> value<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>在 Java 9 之后，String 类的实现改用 byte 数组存储字符串，同时使用 <code>coder</code> 来标识使用了哪种编码。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">class</span> <span class="token class-name">String</span>
    <span class="token keyword">implements</span> <span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span>Serializable</span><span class="token punctuation">,</span> <span class="token class-name">Comparable</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">,</span> <span class="token class-name">CharSequence</span> <span class="token punctuation">{</span>
    <span class="token comment">/** The value is used for character storage. */</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> value<span class="token punctuation">;</span>

    <span class="token comment">/** The identifier of the encoding used to encode the bytes in {@code value}. */</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token keyword">byte</span> coder<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="不可变的好处"><a href="#不可变的好处" class="header-anchor">#</a> 不可变的好处</h3> <p><strong>1. 可以缓存 hash 值</strong></p> <p>因为 String 的 hash 值经常被使用，例如 String 用做 HashMap 的 key。不可变的特性可以使得 hash 值也不可变，因此只需要进行一次计算。</p> <p><strong>2. String Pool 的需要</strong></p> <p>如果一个 String 对象已经被创建过了，那么就会从 String Pool 中取得引用。只有 String 是不可变的，才可能使用 String Pool。</p> <p><strong>3. 安全性</strong></p> <p>String 经常作为参数，String 不可变性可以保证参数不可变。例如在作为网络连接参数的情况下如果 String 是可变的，那么在网络连接过程中，String 被改变，改变 String 的那一方以为现在连接的是其它主机，而实际情况却不一定是。</p> <p><strong>4. 线程安全</strong></p> <p>String 不可变性天生具备线程安全，可以在多个线程中安全地使用。</p> <h3 id="stringbuffer和stringbuilder区别"><a href="#stringbuffer和stringbuilder区别" class="header-anchor">#</a> StringBuffer和StringBuilder区别</h3> <p><strong>1. 可变性</strong></p> <ul><li>String 不可变</li> <li>StringBuffer 和 StringBuilder 可变</li></ul> <p><strong>2. 线程安全</strong></p> <ul><li>String 不可变，因此是线程安全的</li> <li>StringBuilder 不是线程安全的</li> <li>StringBuffer 是线程安全的，内部使用 synchronized 进行同步</li></ul> <h3 id="string-pool"><a href="#string-pool" class="header-anchor">#</a> String Pool</h3> <p>字符串常量池（String Pool）保存着所有字符串字面量（literal strings），这些字面量在编译时期就确定。不仅如此，还可以使用 String 的 intern() 方法在运行过程将字符串添加到 String Pool 中。</p> <p>当一个字符串调用 intern() 方法时，如果 String Pool 中已经存在一个字符串和该字符串值相等（使用 equals() 方法进行确定），那么就会返回 String Pool 中字符串的引用；否则，就会在 String Pool 中添加一个新的字符串，并返回这个新字符串的引用。</p> <p>下面示例中，s1 和 s2 采用 new String() 的方式新建了两个不同字符串，而 s3 和 s4 是通过 s1.intern() 和 s2.intern() 方法取得同一个字符串引用。intern() 首先把 &quot;aaa&quot; 放到 String Pool 中，然后返回这个字符串引用，因此 s3 和 s4 引用的是同一个字符串。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>String s1 = new String(&quot;aaa&quot;);
String s2 = new String(&quot;aaa&quot;);
System.out.println(s1 == s2);           // false
String s3 = s1.intern();
String s4 = s2.intern();
System.out.println(s3 == s4);           // true
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>如果是采用 &quot;bbb&quot; 这种字面量的形式创建字符串，会自动地将字符串放入 String Pool 中。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>String s5 = &quot;bbb&quot;;
String s6 = &quot;bbb&quot;;
System.out.println(s5 == s6);  // true
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>在 Java 7 之前，String Pool 被放在运行时常量池中，它属于永久代。而在 Java 7，String Pool 被移到堆中。这是因为永久代的空间有限，在大量使用字符串的场景下会导致 OutOfMemoryError 错误。</p> <ul><li><a href="https://tech.meituan.com/in_depth_understanding_string_intern.html" target="_blank" rel="noopener noreferrer">深入解析 String#intern<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <p><strong>new String(&quot;abc&quot;)</strong></p> <p>使用这种方式一共会创建两个字符串对象（前提是 String Pool 中还没有 &quot;abc&quot; 字符串对象）。</p> <ul><li>&quot;abc&quot; 属于字符串字面量，因此编译时期会在 String Pool 中创建一个字符串对象，指向这个 &quot;abc&quot; 字符串字面量；</li> <li>而使用 new 的方式会在堆中创建一个字符串对象。</li></ul> <h3 id="字符型常量和字符串常量"><a href="#字符型常量和字符串常量" class="header-anchor">#</a> 字符型常量和字符串常量</h3> <ol><li><strong>形式</strong> : 字符常量是单引号引起的一个字符，字符串常量是双引号引起的 0 个或若干个字符</li> <li><strong>含义</strong> : 字符常量相当于一个整型值( ASCII 值),可以参加表达式运算; 字符串常量代表一个地址值(该字符串在内存中存放位置)</li> <li><strong>占内存大小</strong> ： 字符常量只占 2 个字节; 字符串常量占若干个字节 (<strong>注意： char 在 Java 中占两个字节</strong>)</li></ol> <h2 id="三、运算"><a href="#三、运算" class="header-anchor">#</a> 三、运算</h2> <h3 id="参数传递"><a href="#参数传递" class="header-anchor">#</a> 参数传递</h3> <p>函数参数传递的两种方式——值传递和引用传递</p> <p>值传递包括实实在在的值传递以及指针传递（指针传递参数本质上是值传递的方式，它所传递的是一个地址值），传递的都是实参的一个拷贝。</p> <p>引用传递是对引用的操作等于是对其指定的对象进行操作，当将实参传递给形参时，形参就指向了实参（形参与实参同义，是实参的一个别名）。</p> <p><strong>Java 程序设计语言总是采用按值调用</strong></p> <p><strong>方法得到的是所有参数值的一个拷贝</strong>，<strong>方法不能修改传递给它的任何参数变量的内容。</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> num1 <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> num2 <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span>

    <span class="token function">swap</span><span class="token punctuation">(</span>num1<span class="token punctuation">,</span> num2<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;num1 = &quot;</span> <span class="token operator">+</span> num1<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;num2 = &quot;</span> <span class="token operator">+</span> num2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">swap</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> temp <span class="token operator">=</span> a<span class="token punctuation">;</span>
    a <span class="token operator">=</span> b<span class="token punctuation">;</span>
    b <span class="token operator">=</span> temp<span class="token punctuation">;</span>

    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;a = &quot;</span> <span class="token operator">+</span> a<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;b = &quot;</span> <span class="token operator">+</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p><strong>结果：</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>a = 20
b = 10
num1 = 10
num2 = 20
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><strong>解析：</strong></p> <p><img src="https://gitee.com/linbingxing/image/raw/master/java/base/java_value_swap.jpg" alt="java_value_swap"></p> <p>在 swap 方法中，a、b 的值进行交换，并不会影响到 num1、num2。因为，a、b 中的值，只是从 num1、num2 的复制过来的。也就是说，a、b 相当于 num1、num2 的副本，副本的内容无论怎么修改，都不会影响到原件本身。</p> <p><strong>通过上面例子，我们已经知道了一个方法不能修改一个基本数据类型的参数，而对象引用作为参数就不一样，请看 example2。</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">change</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">change</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> array<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 将数组的第一个元素变为0</span>
        array<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p><strong>结果：</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>1
0
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><strong>解析：</strong></p> <p><img src="https://gitee.com/linbingxing/image/raw/master/java/base/java_value_swap1.jpg" alt="java_value_swap1"></p> <p>array 被初始化 arr 的拷贝也就是一个对象的引用，也就是说 array 和 arr 指向的是同一个数组对象。 因此，外部对引用对象的改变会反映到所对应的对象上。</p> <p><strong>通过 example2 我们已经看到，实现一个改变对象参数状态的方法并不是一件难事。理由很简单，方法得到的是对象引用的拷贝，对象引用及其他的拷贝同时引用同一个对象。</strong></p> <p><strong>很多程序设计语言（特别是，C++和 Pascal)提供了两种参数传递的方式：值调用和引用调用。有些程序员（甚至本书的作者）认为 Java 程序设计语言对对象采用的是引用调用，实际上，这种理解是不对的。由于这种误解具有一定的普遍性，所以下面给出一个反例来详细地阐述一下这个问题。</strong></p> <p><strong>example 3</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// TODO Auto-generated method stub</span>
        <span class="token class-name">Student</span> s1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">&quot;小张&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Student</span> s2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">&quot;小李&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Test</span><span class="token punctuation">.</span><span class="token function">swap</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> s2<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;s1:&quot;</span> <span class="token operator">+</span> s1<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;s2:&quot;</span> <span class="token operator">+</span> s2<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">swap</span><span class="token punctuation">(</span><span class="token class-name">Student</span> x<span class="token punctuation">,</span> <span class="token class-name">Student</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Student</span> temp <span class="token operator">=</span> x<span class="token punctuation">;</span>
        x <span class="token operator">=</span> y<span class="token punctuation">;</span>
        y <span class="token operator">=</span> temp<span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;x:&quot;</span> <span class="token operator">+</span> x<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;y:&quot;</span> <span class="token operator">+</span> y<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p><strong>结果：</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>x:小李
y:小张
s1:小张
s2:小李
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><strong>解析：</strong></p> <p>交换之前：</p> <p><img src="https://gitee.com/linbingxing/image/raw/master/java/base/java_value_swap2.jpg" alt="java_value_swap2"></p> <p>交换之后：</p> <p><img src="https://gitee.com/linbingxing/image/raw/master/java/base/java_value_swap3.jpg" alt="java_value_swap3"></p> <p>通过上面两张图可以很清晰的看出： <strong>方法并没有改变存储在变量 s1 和 s2 中的对象引用。swap 方法的参数 x 和 y 被初始化为两个对象引用的拷贝，这个方法交换的是这两个拷贝</strong></p> <blockquote><p><strong>总结</strong></p></blockquote> <p>Java 程序设计语言对对象采用的不是引用调用，实际上，对象引用是按 值传递的。</p> <p>下面再总结一下 Java 中方法参数的使用情况：</p> <ul><li>一个方法不能修改一个基本数据类型的参数（即数值型或布尔型）。</li> <li>一个方法可以改变一个对象参数的状态。</li> <li>一个方法不能让对象参数引用一个新的对象。</li></ul> <h3 id="float-与-double"><a href="#float-与-double" class="header-anchor">#</a> float 与 double</h3> <p>Java 不能隐式执行向下转型，因为这会使得精度降低。</p> <p>1.1 字面量属于 double 类型，不能直接将 1.1 直接赋值给 float 变量，因为这是向下转型。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// float f = 1.1;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>1.1f 字面量才是 float 类型。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">float</span> f <span class="token operator">=</span> <span class="token number">1.1f</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="隐式类型转换"><a href="#隐式类型转换" class="header-anchor">#</a> 隐式类型转换</h3> <p>因为字面量 1 是 int 类型，它比 short 类型精度要高，因此不能隐式地将 int 类型向下转型为 short 类型。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">short</span> s1 <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token comment">// s1 = s1 + 1;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>但是使用 += 或者 ++ 运算符会执行隐式类型转换。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>s1 <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">;</span>
s1<span class="token operator">++</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面的语句相当于将 s1 + 1 的计算结果进行了向下转型：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>s1 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">short</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>s1 <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="switch"><a href="#switch" class="header-anchor">#</a> switch</h3> <p>从 Java 7 开始，可以在 switch 条件判断语句中使用 String 对象。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>String s = &quot;a&quot;;
switch (s) {
    case &quot;a&quot;:
        System.out.println(&quot;aaa&quot;);
        break;
    case &quot;b&quot;:
        System.out.println(&quot;bbb&quot;);
        break;
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>switch 不支持 long、float、double，是因为 switch 的设计初衷是对那些只有少数几个值的类型进行等值判断，如果值过于复杂，那么还是用 if 比较合适。</p> <h2 id="四、关键字"><a href="#四、关键字" class="header-anchor">#</a> 四、关键字</h2> <h3 id="final"><a href="#final" class="header-anchor">#</a> final</h3> <p><strong>1. 数据</strong></p> <p>声明数据为常量，可以是编译时常量，也可以是在运行时被初始化后不能被改变的常量。</p> <ul><li>对于基本类型，final 使数值不变；</li> <li>对于引用类型，final 使引用不变，也就不能引用其它对象，但是被引用的对象本身是可以修改的。</li></ul> <div class="language- line-numbers-mode"><pre class="language-text"><code>final int x = 1;
// x = 2;  // cannot assign value to final variable 'x'
final A y = new A();
y.a = 1;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><strong>2. 方法</strong></p> <p>声明方法不能被子类重写。</p> <p>private 方法隐式地被指定为 final，如果在子类中定义的方法和基类中的一个 private 方法签名相同，此时子类的方法不是重写基类方法，而是在子类中定义了一个新的方法。</p> <p><strong>3. 类</strong></p> <p>声明类不允许被继承。</p></div></div> <div class="page-slot page-slot-bottom"><!-- 横向自适应 -->
      <ins class="adsbygoogle"
          style="display:block"
          data-ad-client="ca-pub-7828333725993554"
          data-ad-slot="6620245489"
          data-ad-format="auto"
          data-full-width-responsive="true"></ins>
      <script>
          (adsbygoogle = window.adsbygoogle || []).push({});
      </script></div> <div class="page-edit"><div class="edit-link"><a href="https://github.com/linbingxing/vuepress-theme-vdoing/edit/master/docs/07.Java/01.基础/02.知识点.md" target="_blank" rel="noopener noreferrer">编辑</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2021/11/29, 10:58:58</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/a23428/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">Java 基础 - 面向对象</div></a> <a href="/pages/6c6098/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">Java 基础 - 反射机制详解</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/a23428/" class="prev">Java 基础 - 面向对象</a></span> <span class="next"><a href="/pages/6c6098/">Java 基础 - 反射机制详解</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/e7004a/"><div>WeakHashMap源码解析</div></a> <span>11-28</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/e4a61a/"><div>TreeSet &amp; TreeMap 源码解析</div></a> <span>11-28</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/7bd9aa/"><div>LinkedHashSet&amp;Map源码解析</div></a> <span>11-28</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:linbingxing@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/linbingxing" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2020-2021
    <span>林半码 | <a href="https://gitee.com/linbingxing/vuepress_doc/blob/master/LICENSE" target="_blank">MIT License</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <div class="custom-html-window custom-html-window-rb" style="display:;"><div class="custom-wrapper"><i class="close-but">×</i> <div><!-- 固定160*160px -->
      <ins class="adsbygoogle"
          style="display:inline-block;max-width:160px;max-height:160px"
          data-ad-client="ca-pub-7828333725993554"
          data-ad-slot="8377369658"></ins>
      <script>
          (adsbygoogle = window.adsbygoogle || []).push({});
      </script>
      </div></div></div></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.66ec18f0.js" defer></script><script src="/assets/js/2.94569d7c.js" defer></script><script src="/assets/js/79.0e8351bf.js" defer></script>
  </body>
</html>
